package com.youcan.search.filter;


import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.core.SolrCore;
import org.apache.solr.servlet.SolrDispatchFilter;

import com.youcan.search.SolrServerWrapper;


public class CustomSolrDispatchFilter extends SolrDispatchFilter {

	private static final Log logger = LogFactory
			.getLog(CustomSolrDispatchFilter.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig config) throws ServletException {
		System.out.println("Customized solr dispath filter is initializing...");
		logger.info("Customized solr dispath filter is initializing...");
		boolean abortOnConfigurationError = true;
		List<Throwable> list = new ArrayList<Throwable>();
		try {
			ServletContext ctx = config.getServletContext();
			String pdctHome = ctx.getRealPath("") + File.separatorChar;
			this.cores = SolrServerWrapper.initContainer(pdctHome);
			// web.xml configuration
			this.pathPrefix = config.getInitParameter("path-prefix");
			String aoce = config.getInitParameter("abortOnConfigError");
			abortOnConfigurationError = (aoce == null || aoce.trim().length() == 0) ? abortOnConfigurationError
					: Boolean.valueOf(aoce);
			logger.info("user.dir=" + System.getProperty("user.dir"));
		} catch (Throwable t) {
			// catch this so our filter still works
			logger.error("Could not start Solr. Check solr/home property", t);
			list.add(t);
			SolrCore.log(t);
		}

		// Optionally abort if we found a sever error
		if (abortOnConfigurationError && list.size() > 0) {
			StringWriter sw = new StringWriter();
			PrintWriter out = new PrintWriter(sw);
			out.println("Severe errors in solr configuration.\n");
			out.println("Check your log files for more detailed information on what may be wrong.\n");
			out.println("If you want solr to continue after configuration errors, change: \n");
			out.println(" <abortOnConfigurationError>false</abortOnConfigurationError>\n");
			out.println("in solr.xml \n");

			for (Throwable t : list) {
				out.println("-------------------------------------------------------------");
				t.printStackTrace(out);
			}
			out.flush();

			// Servlet containers behave slightly differently if you throw an
			// exception during
			// initialization. Resin will display that error for every page,
			// jetty prints it in
			// the logs, but continues normally. (We will see a 404 rather then
			// the real error)
			// rather then leave the behavior undefined, lets cache the error
			// and spit it out
			// for every request.
			abortErrorMessage = sw.toString();
			// throw new ServletException( abortErrorMessage );
		}

		logger.info("Customized solr dispatch filter initialization is done.");

	}

}
